package com.aispeech.asr.clib;

import com.aispeech.asr.clib.post.AlignInfo;
import com.aispeech.asr.clib.post.Post;
import com.aispeech.asr.clib.post.PostEnv;
import com.aispeech.asr.clib.post.PostResult;
import lombok.extern.java.Log;

import java.io.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Log
public class PostApp {
    public static void main(String[] args) throws InterruptedException {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            log.info("begin shutdown...");
            Post.destroy();
        }));
        int threads = 2;

        if (args.length < 3) {
            log.warning("[ ERROR ] too few arguments.");
            log.warning("Usage:");
            log.warning("java -cp <xx>.jar com.aispeech.asr.clib.PostApp <RES_ROOT> <CUDA_UNIX_PATH> <ALIGN_DIR>");
            System.exit(1);
        } else {
            if (args.length >= 4)
                threads = Integer.parseInt(args[3]);
        }

        ExecutorService pool = Executors.newFixedThreadPool(threads);

        Post.initialize(args[0], args[1]);

        PostEnv env = new PostEnv(false, true, true, true, false, false, false, null, null);

        /*
        File alignDir = new File(args[2]);
        if (alignDir.exists() && alignDir.isDirectory()) {
            File[] files = alignDir.listFiles();

            log.info(">>>>>>> Begin walk dir: " + alignDir.getAbsolutePath() + ", files: " + files.length);

            for (File fileAlign : files) {
//                doPost(env, fileAlign);
                pool.execute(()->{
                    doPost(env, fileAlign);
                });
            }
        } else {
            log.warning("dir " + args[2] + " not exists or not dir.");
        }
        */

        for (int i = 0; i < 100000; i++) {
            pool.execute(() -> { doPost_aaa(env); });
        }

        pool.shutdown();
        pool.awaitTermination(8, TimeUnit.HOURS);
    }
    private static void doPost_aaa(PostEnv env) {
            //log.info(String.format("[ doPost ] begin..."));
            AlignInfo alignInfo = new AlignInfo();
            //alignInfo.addElement(0, 540, "0", "杨鹏先森", '\0');

            alignInfo.addElement(130, 540, "0", "嗯", '\0');
            alignInfo.addElement(970, 1410, "0", "嗯", '\0');
            alignInfo.addElement(1410, 2060, "0", "嗯", '\0');
            alignInfo.addElement(2860, 3220, "0", "嗯", '\0');
            alignInfo.addElement(4180, 4290, "0", "啊", '\0');
            alignInfo.addElement(4290, 4440, "0", "啊", '\0');
            alignInfo.addElement(4440, 4500, "0", "啊", '\0');
            alignInfo.addElement(4530, 6610, "0", "啊", '\0');
            alignInfo.addElement(6650, 6680, "0", "啊", '\0');
            alignInfo.addElement(6680, 10880, "0", "啊", '\0');
            alignInfo.addElement(10910, 11790, "0", "啊", '\0');
            alignInfo.addElement(12110, 13110, "0", "啊", '\0');
            alignInfo.addElement(13840, 14010, "0", "啊", '\0');
            alignInfo.addElement(15590, 15820, "0", "啊", '\0');
            alignInfo.addElement(16030, 16610, "0", "啊", '\0');
            alignInfo.addElement(16640, 17380, "0", "啊", '\0');
            alignInfo.addElement(17410, 17510, "0", "啊", '\0');

            /*
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            */
            PostResult result = Post.process(alignInfo, env);
            //log.info(String.format("[ doPost ] status: %d", 1));
            //log.info(String.format("[ doPost ] status: %d", result.getStatus()));
    }

    private static void doPost(PostEnv env, File fileAlign) {
        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(fileAlign));
            String line = null;
            AlignInfo alignInfo = new AlignInfo();

            while ((line = reader.readLine()) != null) {
                String[] row = line.split(" ", 4);
                alignInfo.addElement(
                        Integer.parseInt(row[0]),
                        Integer.parseInt(row[1]),
                        "0",
null,
//                        row[3],
                        '\0'
                );
            }
            log.info(String.format("[ doPost ] begin fpath: %s", fileAlign.getAbsolutePath()));
            PostResult result = Post.process(alignInfo, env);
            log.info(String.format("[ doPost ] status: %d, fpath: %s", result.getStatus(), fileAlign.getAbsolutePath()));
        } catch (FileNotFoundException ex) {
            log.warning("file not found. path: " + fileAlign.getAbsolutePath());
        } catch (IOException ex) {
            log.warning(String.format("file: %s, exception: %s", fileAlign.getAbsolutePath(), ex.getMessage()));
        }

        try {
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
